<?php
include("./include/config.php");
include("./include/func.php");
include("./include/db.php");

$db = new db();
if (!$db->connect()) exit(mysql_error);

if (isset($_POST['regio'])) {

    $p_parent = (int)$_POST['parent'];
    $p_nev    = trim($_POST['nev']);
	$p_slug   = trim($_POST['slug']);
	
    $success = true;
    $errors = array();

    if ($p_nev != '' && ! preg_match ('/^[\w- ]*$/u', $p_nev)) {
        $success = false;
        array_push($errors, "Invalid név!");
    }

    if ($p_nev == '') {
        $success = false;
        array_push($errors, "The név field is required !");
    }

    if ($p_slug != '' && ! preg_match ('/^[a-zA-Z0-9_-]*$/', $p_slug)) {
        $success = false;
        array_push($errors, "Invalid slug!");
    }

    if ($p_slug == '') {
        $success = false;
        array_push($errors, "The slug field is required !");
    }

    if ($success) {

        $db->sql = "INSERT INTO regio (id,nev,slug,parent) VALUES ('','$p_nev','$p_slug','$p_parent');";
        $db->query();

        $db->sql = "SELECT last_insert_id()";
        $db->query();
        $rs = mysql_fetch_row($db->rs);
        $last = (int)$rs[0];
        $db->sql = "UPDATE regio SET `order`='$last' WHERE id='$last'";
        $db->query();
    }
}

if (isset ($_POST['regio_slug_generate'])) {
	
	$p_nev = trim ($_POST['nev']);

    $success = false;
    $errors = array();
    	
    if (($p_nev == '') || ($p_nev != '' && ! preg_match ('/^[\w- ]*$/u', $p_nev))) {
        $success = false;
        array_push($errors, "Invalid név!");
    } else {
		$p_slug = slug ($p_nev);
	}
	
}

$db->sql = "SELECT id,nev,slug FROM regio WHERE parent=0 ORDER BY `order`";
$db->query();

?>

<?php include ("header.php"); ?>
	
<p><ul id='breadcrumb'><li><a href='index.php' title='Home'><img src='images/home.png' alt='Home' class='home' /></a></li><li><a href='regio_list.php' title='regio-List'>RÃ©giÃ³</a></li><li><a href='regio_create.php' title='regio-Create'>Create</a></li></ul></p>

<br />

<div id="wrapper">

<div id='menu'><ul class='first-level'><li><a href='rovat_list.php' title='Rovat'>Rovat</a></li><ul class='second-level'><li><a href='rovat_create.php' title='Rovat-Create'>Create</a></li><li><a href='rovat_list.php' title='Rovat-List'>List</a></li></ul></ul><ul class='first-level'><li><a href='regio_list.php' title='RÃ©giÃ³'>RÃ©giÃ³</a></li><ul class='second-level'><li><a href='regio_create.php' title='RÃ©giÃ³-Create'>Create</a></li><li><a href='regio_list.php' title='RÃ©giÃ³-List'>List</a></li></ul></ul><ul class='first-level'><li><a href='lejarat_list.php' title='LejÃ¡rat'>LejÃ¡rat</a></li><ul class='second-level'><li><a href='lejarat_create.php' title='LejÃ¡rat-Create'>Create</a></li><li><a href='lejarat_list.php' title='LejÃ¡rat-List'>List</a></li></ul></ul><ul class='first-level'><li><a href='hirdetes_list.php' title='HirdetÃ©s'>HirdetÃ©s</a></li><ul class='second-level'><li><a href='hirdetes_create.php' title='HirdetÃ©s-Create'>Create</a></li><li><a href='hirdetes_list.php' title='HirdetÃ©s-List'>List</a></li></ul></ul><ul class='first-level'><li><a href='kiemeles_list.php' title='KiemelÃ©s'>KiemelÃ©s</a></li><ul class='second-level'><li><a href='kiemeles_create.php' title='KiemelÃ©s-Create'>Create</a></li><li><a href='kiemeles_list.php' title='KiemelÃ©s-List'>List</a></li></ul></ul></div>

<div id="content">
	
<form name="form_regio" id="form_regio" method="post" enctype='application/x-www-form-urlencoded' accept-charset="UTF-8" class="form">

<h2>Create Régió</h2>

<br />

<?php
if (isset($success)) {
    if (!$success) {
        print "<ul class='errors'>";
        foreach ($errors as $err) print "<li>$err</li>";
        print "</ul>";
    } else print "<p class='success'>A régió sikeresen létrejött!</p>";;
}
?>

<label for='parent'>Szülő</label>

<select name='parent'>
<option value='0'>Nincs</option>
<?php while ($row = mysql_fetch_array($db->rs)) print "<option value='" . $row['id'] . "' " . ((isset($p_parent) && ! $success) ? "selected='true'" : '') . ">" . $row['nev'] . "</option>"; ?>
</select>
<br />

<label for='nev'>Név</label>
<input name='nev' type='text' <?php if (isset($p_nev) && !$success) print "value='$p_nev'"; ?> />
<p class='note'>Required</p>
<br />

<label for="slug">Slug</label>
<input type="text" name="slug" size="50" maxlength="50" <?php if (isset ($p_slug) && ! $success) print "value='$p_slug'"; ?> />
<span><input type="submit" name="regio_slug_generate" value="Generate"/></span>
<p class="note">Required</p>
<br />

<label for="regio"></label>
<input type="submit" name="regio" value="Add"/>

</form>

</div>

</div>

<?php include ("footer.php"); ?>
